# -*- coding: UTF-8 -*-
import scrapy
import messAround.util.help as util


# B站-热门排行榜
# scrapy crawl bilibili_hot
# https://api.bilibili.com/x/web-interface/popular?ps=40&pn=1
class BilibiliRankSpider(scrapy.Spider):
    name = 'bilibili_hot'

    allowed_domains = ['bilibili.com']

    start_urls = ['http://bilibili.com/']

    headers = {
        'User-Agent': util.agent,
        ':method': 'GET',
        ':authority': 'api.bilibili.com',
        ':path': '/x/web-interface/popular?ps=20&pn=1',
        ':scheme': 'https',
        'referer': 'https://www.bilibili.com/',
        'origin': 'https://www.bilibili.com',
        'sec-ch-ua': ' Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91',
        'sec-ch-ua-mobile': '?0',
        'Sec-Fetch-mode': 'cors',
        'Sec-Fetch-site': 'same-origin',
    }

    custom_settings = {
        'ITEM_PIPELINES': {
            'messAround.pipeline.bilibili.BiliBiliRankPipeline': 300
        }
    }

    def start_requests(self):
        url = "https://api.bilibili.com/x/web-interface/popular?ps=40&pn=1"
        yield scrapy.Request(url=url, headers=util.default_headers,
                             callback=self.parse)

    def parse(self, response):
        result = util.make_json(response.text)
        if result['message'] == "0":
            for key, val in enumerate(result['data']['list']):
                yield {
                    'no': key + 1,
                    'title': val['title'],
                    'info': val['desc'],
                    'channel': val['tname'],
                    'cover': val['pic'],
                    'dynamic': val['dynamic'],
                    'video': val['short_link'],
                    'video_id': val['bvid'],
                    'video_duration': int(val['duration']),
                    'play': int(val['stat']['view']),
                    'danmaku': int(val['stat']['danmaku']),
                    'share': int(val['stat']['share']),
                    'like': int(val['stat']['like']),
                    'reply': int(val['stat']['reply']),
                    'coin': int(val['stat']['coin']),
                    'author_id': int(val['owner']['mid']),
                    'author_avatar': val['owner']['face'],
                    'author_name': val['owner']['name'],
                    'create_time': int(val['ctime']),
                }
